/**
 * 工具栏管理模块
 * 负责管理右上角的功能：全屏，用户
 */
const ToolManager = {
    // DOM引用
    fullscreenButton: null,
    userDropdownButton: null,
    userDropdownMenu: null,

    // 状态
    dropdownHideTimeout: null, // 隐藏下拉菜单的定时器
    isDropdownHovered: false, // 下拉菜单是否被鼠标悬停

    /**
     * 初始化工具栏管理器
     */
    init() {
        // 获取DOM引用
        this.fullscreenButton = document.getElementById('fullscreenButton');
        this.userDropdownButton = document.getElementById('userDropdownButton');
        this.userDropdownMenu = document.querySelector('.user-dropdown-menu');

        // 设置事件监听
        this._setupEvents();
    },

    /**
     * 设置事件监听
     * @private
     */
    _setupEvents() {
        // 全屏按钮点击事件
        if (this.fullscreenButton) {
            Utils.on(this.fullscreenButton, 'click', () =>
                this._toggleFullscreen()
            );
        }

        // 用户下拉菜单切换事件
        if (this.userDropdownButton && this.userDropdownMenu) {
            Utils.on(this.userDropdownButton, 'click', () =>
                this._toggleUserDropdown()
            );

            // 添加鼠标离开按钮事件监听
            Utils.on(this.userDropdownButton, 'mouseleave', () => {
                // 只有当菜单显示且鼠标不在菜单上时才关闭
                if (this.userDropdownMenu.classList.contains('show') && !this.isDropdownHovered) {
                    this._hideDropdownMenu();
                }
            });

            // 添加鼠标进入下拉菜单事件监听
            Utils.on(this.userDropdownMenu, 'mouseenter', () => {
                if (this.dropdownHideTimeout) {
                    clearTimeout(this.dropdownHideTimeout);
                }
                this.isDropdownHovered = true;
            });

            // 添加鼠标离开下拉菜单事件监听
            Utils.on(this.userDropdownMenu, 'mouseleave', () => {
                this.isDropdownHovered = false;
                this._hideDropdownMenu();
            });

            // 添加点击下拉菜单项事件监听
            Utils.on(document.getElementById('userProfile'), 'click', () => {
                this._hideDropdownMenuImmediately();
            });

            Utils.on(document.getElementById('changePwd'), 'click', () => {
                this._hideDropdownMenuImmediately();
            });

            Utils.on(document.getElementById('userLogout'), 'click', () => {
                this._hideDropdownMenuImmediately();
            });
        }

        // 监听全屏状态变化
        Utils.on(document, 'fullscreenchange', () =>
            this._updateFullscreenIcon()
        );
    },

    /**
     * 切换全屏状态
     * @private
     */
    _toggleFullscreen() {
        if (!document.fullscreenElement) {
            document.documentElement.requestFullscreen().catch((err) => {
                console.error(`全屏请求错误: ${err.message}`);
            });
        } else {
            if (document.exitFullscreen) {
                document.exitFullscreen();
            }
        }
    },

    /**
     * 更新全屏按钮图标
     * @private
     */
    _updateFullscreenIcon() {
        if (!this.fullscreenButton) return;

        const isFullscreen = !!document.fullscreenElement;
        const iconElement = this.fullscreenButton.querySelector('i');
        if (iconElement) {
            iconElement.className = isFullscreen
                ? 'fas fa-compress'
                : 'fas fa-expand';
        }
    },

    /**
     * 切换用户下拉菜单显示状态
     * @private
     */
    _toggleUserDropdown() {
        if (this.userDropdownMenu && this.userDropdownButton) {
            const isShowing = this.userDropdownMenu.classList.toggle('show');

            // 切换下拉箭头图标
            const arrowIcon = this.userDropdownButton.querySelector('.dropdown-arrow');
            if (arrowIcon) {
                arrowIcon.className = isShowing
                    ? 'fas fa-chevron-up dropdown-arrow'
                    : 'fas fa-chevron-down dropdown-arrow';
            }
        }
    },

    /**
     * 处理下拉菜单鼠标离开事件，延迟关闭下拉菜单
     * @private
     */
    _hideDropdownMenu() {
        if (this.dropdownHideTimeout) {
            clearTimeout(this.dropdownHideTimeout);
        }
        this.dropdownHideTimeout = setTimeout(() => {
            if (!this.isDropdownHovered && this.userDropdownMenu && this.userDropdownButton) {
                // 检查菜单是否正在显示，是则关闭并更新图标
                if (this.userDropdownMenu.classList.contains('show')) {
                    this.userDropdownMenu.classList.remove('show');

                    // 恢复下拉箭头图标为向下
                    const arrowIcon = this.userDropdownButton.querySelector('.dropdown-arrow');
                    if (arrowIcon) {
                        arrowIcon.className = 'fas fa-chevron-down dropdown-arrow';
                    }
                }
            }
        }, 200);
    },

    _hideDropdownMenuImmediately() {
        if (this.dropdownHideTimeout) {
            clearTimeout(this.dropdownHideTimeout);
        }
        if (this.userDropdownMenu && this.userDropdownButton) {
            this.userDropdownMenu.classList.remove('show');
            const arrowIcon = this.userDropdownButton.querySelector('.dropdown-arrow');
            if (arrowIcon) {
                arrowIcon.className = 'fas fa-chevron-down dropdown-arrow';
            }
        }
    },
};

// 导出ToolManager对象供外部使用
if (typeof module !== 'undefined') {
    module.exports = { ToolManager };
}
